package com.tian.hello.ipc

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color.RED
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

class MCustomView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyle: Int = 0
) : View(context, attrs, defStyle) {

    private val paint = Paint().apply {
        color = RED
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val rectWith = width * (2 / 3f)
        val rectHeight = height * (2 / 3f)
        val left = (width - rectWith) * 0.5f
        val top = (height - rectHeight) * 0.5f

        canvas.drawRect(left, top, width - left, height - top, paint)
    }

}